package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.PlayingField;


import com.google.gson.Gson;

import controller.ArtificialSolver;
import controller.MoveBackward;
import controller.MoveForward;
import controller.NoMoveBackwardPossible;
import controller.NoMoveForwardPossible;



/**
 * Servlet implementation class PlayFieldServlet
 */
@WebServlet("/PlayFieldServlet")
public class PlayFieldServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	public static PlayingField pf;
	public static ArtificialSolver as;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PlayFieldServlet() {
        super();
        pf = new PlayingField();
        as = new ArtificialSolver();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//PlayingField pf = new PlayingField();
		//pf.setToFireplaceStart();
		pf = new PlayingField();
		String config = request.getParameter("config") != null? request.getParameter("config") : "";
		switch(config){
			case "arrow": pf.setToArrowStart(); break;
			case "diamond": pf.setToDiamondStart(); break;
			case "fire": pf.setToFireplaceStart(); break;
			case "pyramid": pf.setToPyramidStart(); break;
			case "small": pf.setToSmallerCrossStart(); break;
			case "x": pf.setToXStart(); break;
		}
		int[][] m = pf.getThreeStatesPlayingField();
		PrintWriter out = response.getWriter();
		Gson gsonObj = new Gson();
        String playFieldAsJSON = gsonObj.toJson(m);
        out.println(playFieldAsJSON);
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		boolean undo = Boolean.parseBoolean(request.getParameter("undo"));
		boolean redo = Boolean.parseBoolean(request.getParameter("redo"));		
		
		try {
			if(undo) 
				undo();
			if(redo) 
				redo();
			if(!undo && !redo){ //Is movement
				int r0 = Integer.parseInt(request.getParameter("r0"));
				int c0 = Integer.parseInt(request.getParameter("c0"));
				int r1 = Integer.parseInt(request.getParameter("r1"));
				int c1 = Integer.parseInt(request.getParameter("c1"));
				pf = as.moveForward(pf, c0, r0, c1, r1);
			}  
				
		}catch(NoMoveForwardPossible e) {
			System.out.print(e.getMessage());	
		}catch(NoMoveBackwardPossible e) {
			System.out.print(e.getMessage());	
		}finally{
			int[][] m = pf.getThreeStatesPlayingField();
			
			PrintWriter out = response.getWriter();
			Gson gsonObj = new Gson();
			String playFieldAsJSON = gsonObj.toJson(m);
			out.println(playFieldAsJSON);
			out.close();
		}
	}
	
	
	public void undo() throws NoMoveForwardPossible{
		MoveForward mf = pf.getLastMoveForward();
		if(mf != null) {
				pf = as.moveBackward(pf, mf.getxEnd(), mf.getyEnd(), mf.getxStart(), mf.getyStart());
		}
	}
	
	public void redo() throws NoMoveBackwardPossible{
		MoveBackward mb = pf.getLastMoveBackward();
		if(mb != null) {
				pf = as.moveForward(pf, mb.getxEnd(), mb.getyEnd(), mb.getxStart(), mb.getyStart());
		}
	}

}
